home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / gfx / ycur2iff.lha / ycur2iff / ycur2iff.c < prev   
C/C++ Source or Header  |  1995-09-04  |  10KB  |  359 lines

  1. /*==== Header ==============================================================
  2. **
  3. ** Filename ......... ycur2iff.c
  4. ** Purpose .......... Converts .CUR (WinNT cursor format) to iff pics
  5. ** Coder ............ Yoda
  6. ** Start date ....... 03-Sep-95
  7. ** Version ..........
  8. ** Last ver. date ...
  9. **
  10. **==========================================================================
  11. **
  12. **    Copyright (C) 1991  Rodrigo Ventura
  13. **
  14. **    This program is free software; you can redistribute it and/or modify
  15. **    it under the terms of the GNU General Public License as published by
  16. **    the Free Software Foundation; either version 2 of the License, or
  17. **    (at your option) any later version.
  18. **
  19. **    This program is distributed in the hope that it will be useful,
  20. **    but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. **    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. **    GNU General Public License for more details.
  23. **
  24. **    You should have received a copy of the GNU General Public License
  25. **    along with this program; if not, write to the Free Software
  26. **    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. **
  28. **==========================================================================
  29. */
  30.  
  31. char version[] = "$VER: ycur2iff 1.0 (C) 1995 Yoda";
  32.  
  33. /*========================================================================*/
  34. /*=== Includes ===========================================================*/
  35. /*========================================================================*/
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <exec/types.h>
  41. #include <dos/dos.h>
  42. #include <dos/doshunks.h>
  43. #include <libraries/iffparse.h>
  44. #include <datatypes/pictureclass.h> /* Just for BMHD struct */
  45. #include <proto/dos.h>
  46. #include <proto/exec.h>
  47. #include <proto/iffparse.h>
  48.  
  49. /*========================================================================*/
  50. /*=== Macros =============================================================*/
  51. /*========================================================================*/
  52.  
  53.  
  54. #define WI 32     /* Width */
  55. #define HE 32     /* Height */
  56. #define DE 4      /* Depth */
  57. #define NC 16     /* # of colors */
  58. #define PO 62     /* Palette offset */
  59. #define CH 126    /* CUR's header */
  60. #define AH 146    /* ANI's header */
  61. #define IH 8      /* icon's pre-header */
  62.  
  63. #define PS (WI*HE/8)            /* Plane size */
  64. #define IS (IH+CH+WI*HE/2+PS)   /* Icon size */
  65.  
  66. /* This is #define-o-mania ! 8-) */
  67. #define ID_AUTH MAKE_ID('A','N','N','O')
  68. #define RIFF_NAME "RIFF"
  69. #define RIFF_SIZE 4
  70. #define POINT '.'
  71. #define NUL '\0'
  72. #define EXT_SIZE 4
  73.  
  74. #define BTST(b,x) (((x)&(1<<(b)))>0)
  75. #define BSET(b,x) ((x)|=(1<<(b)))
  76.  
  77.  
  78. /*========================================================================*/
  79. /*=== Data ===============================================================*/
  80. /*========================================================================*/
  81.  
  82.  
  83. char fileName[FILENAME_MAX] = "";
  84. BPTR fileHandle=NULL;
  85. UBYTE *fileBuffer=NULL;
  86. ULONG fileSize=0;
  87. int isAnim;
  88.  
  89.  
  90. /*========================================================================*/
  91. /*=== Prototypes =========================================================*/
  92. /*========================================================================*/
  93.  
  94. int main( int argc, char **argv );
  95. void parseArgs( void );
  96. int readFile( void );
  97. void freeBuffer( void );
  98. void convertIcon( UBYTE *start );
  99. void convertAnim( void );
  100. void stripFilename( char *name );
  101.  
  102. /*========================================================================*/
  103. /*=== Functions ==========================================================*/
  104. /*========================================================================*/
  105.  
  106.  
  107. int main( int argc, char **argv )
  108. {
  109.   parseArgs();
  110.   if ( readFile() )
  111.   {
  112.     stripFilename( fileName );
  113.     if ( isAnim )
  114.       convertAnim();
  115.     else
  116.       convertIcon( fileBuffer );
  117.   }
  118.   freeBuffer();
  119. }
  120.  
  121.  
  122. /*------------------------------------------------------------------------*/
  123.  
  124.  
  125. void parseArgs( void )
  126. {
  127.   char *name=NULL;
  128.   struct RDArgs *args;
  129.  
  130.   args = ReadArgs( "FILE/A", (LONG *)&name, NULL );
  131.   if ( name )
  132.     strcpy( fileName, name );
  133.   else
  134.   {
  135.     printf(
  136. "-- ycur2iff -- .CUR (WinNT) to IFF converter\n"
  137. "            (C) 1995 Master Yoda\n"
  138. "Usage:\n"
  139. "  ycur2iff <filename>\n" );
  140.     fileName[0] = '\0';
  141.   }
  142.   FreeArgs( args );
  143. }
  144.  
  145.  
  146. /*------------------------------------------------------------------------*/
  147.  
  148.  
  149. int readFile( void )
  150. {
  151.   struct FileInfoBlock fib;
  152.  
  153.   fileHandle = Open( fileName, MODE_OLDFILE );
  154.   if ( fileHandle )
  155.   {
  156.     printf( "Reading file '%s'", fileName );
  157.     fflush( stdout );
  158.     ExamineFH( fileHandle, &fib );
  159.     fileSize = fib.fib_Size;
  160.     fileBuffer = malloc( fileSize );
  161.     if ( fileBuffer )
  162.     {
  163.       Read( fileHandle, fileBuffer, fileSize );
  164.       printf( " (%ld bytes).\n", fileSize );
  165.       fflush( stdout );
  166.       isAnim = ( memcmp(RIFF_NAME,fileBuffer,RIFF_SIZE) == 0 );
  167.       Close( fileHandle );
  168.       return 1;
  169.     }
  170.     else
  171.       printf( " - No memory to read %ld bytes !\n", fileSize );
  172.     Close( fileHandle );
  173.   }
  174.   else
  175.     printf( "File '%s' not found !\n", fileName );
  176.  
  177.   return 0;
  178. }
  179.  
  180.  
  181. /*------------------------------------------------------------------------*/
  182.  
  183.  
  184. void freeBuffer( void )
  185. {
  186.   if ( fileBuffer ) free(fileBuffer);
  187. }
  188.  
  189.  
  190. /*------------------------------------------------------------------------*/
  191.  
  192.  
  193. void convertIcon( UBYTE *start )
  194. {
  195.   static char name[FILENAME_MAX];
  196.   BPTR file;
  197.   struct IFFHandle *iff;
  198.   static struct BitMapHeader bmhdBody =
  199.   {
  200.     WI, HE, 0, 0, DE,
  201.     mskNone, cmpNone, 0, 0,
  202.     1, 1, HE, WI
  203.   };
  204.   static struct BitMapHeader bmhdMask =
  205.   {
  206.     WI, HE, 0, 0, 1,
  207.     mskNone, cmpNone, 0, 0,
  208.     1, 1, WI, HE
  209.   };
  210.   static struct ColorRegister cmap[NC];
  211.   static UBYTE bodyPlane[HE][DE][WI/8];
  212.   register int p, o, b;
  213.   register UBYTE *ptr;
  214.  
  215.   ptr = start + CH;
  216.   sprintf( name, "%s.body.iff", fileName );
  217.   file = Open( name, MODE_NEWFILE );
  218.   if ( file )
  219.   {
  220.     printf( "Writing body to '%s'.\n", name );
  221.  
  222.     iff = AllocIFF();
  223.     if ( iff )
  224.     {
  225.       iff->iff_Stream = file;
  226.       InitIFFasDOS( iff );
  227.       OpenIFF(iff,IFFF_WRITE);
  228.  
  229.       PushChunk( iff, ID_ILBM, ID_FORM, IFFSIZE_UNKNOWN );
  230.  
  231.       PushChunk( iff, ID_ILBM, ID_BMHD, sizeof(struct BitMapHeader) );
  232.       WriteChunkBytes( iff, &bmhdBody, sizeof(bmhdBody) );
  233.       PopChunk( iff );
  234.  
  235.       PushChunk( iff, ID_ILBM, ID_AUTH, sizeof(version)-5 );
  236.       WriteChunkBytes( iff, version+5, sizeof(version)-5 );
  237.       PopChunk( iff );
  238.  
  239.       PushChunk( iff, ID_ILBM, ID_CMAP, sizeof(cmap) );
  240.       for ( p=0 ; p<NC ; p++ )
  241.       {
  242.         cmap[p].blue  = start[ PO+4*p+0 ];
  243.         cmap[p].green = start[ PO+4*p+1 ];
  244.         cmap[p].red = start[ PO+4*p+2 ];
  245.       }
  246.       WriteChunkBytes( iff, &cmap, sizeof(cmap) );
  247.       PopChunk( iff );
  248.  
  249.       PushChunk( iff, ID_ILBM, ID_BODY, IFFSIZE_UNKNOWN );
  250.       for ( p=0 ; p<DE ; p++ )
  251.         for ( o=0 ; o<PS ; o++ )
  252.         {
  253.           register UBYTE planar, chunky;
  254.  
  255.           planar = 0;
  256.           for ( b=0 ; b<8 ; b++ )
  257.           {
  258.             chunky = ptr[4*o+b/2];
  259.             if ( BTST(4*(b%2)+p,chunky) ) BSET(7-b,planar);
  260.           }
  261.           bodyPlane[o/(WI/8)][p][o%(WI/8)] = planar;
  262.         }
  263.       WriteChunkBytes( iff, bodyPlane, sizeof(bodyPlane) );
  264.       PopChunk( iff );
  265.  
  266.       PopChunk( iff );
  267.       CloseIFF( iff );
  268.       FreeIFF( iff );
  269.     }
  270.     Close( file );
  271.   }
  272.   else
  273.     printf(" - Can't open body file '%s' !\n", name );
  274.  
  275.   ptr += HE*WI/2;
  276.   sprintf( name, "%s.mask.iff", fileName );
  277.   file = Open( name, MODE_NEWFILE );
  278.   if ( file )
  279.   {
  280.     printf( "Writing mask to '%s'.\n", name );
  281.  
  282.     iff = AllocIFF();
  283.     if ( iff )
  284.     {
  285.       iff->iff_Stream = file;
  286.       InitIFFasDOS( iff );
  287.       OpenIFF(iff,IFFF_WRITE);
  288.  
  289.       PushChunk( iff, ID_ILBM, ID_FORM, IFFSIZE_UNKNOWN );
  290.  
  291.       PushChunk( iff, ID_ILBM, ID_BMHD, sizeof(struct BitMapHeader) );
  292.       WriteChunkBytes( iff, &bmhdMask, sizeof(bmhdMask) );
  293.       PopChunk( iff );
  294.  
  295.       PushChunk( iff, ID_ILBM, ID_AUTH, sizeof(version)-5 );
  296.       WriteChunkBytes( iff, version+5, sizeof(version)-5 );
  297.       PopChunk( iff );
  298.  
  299.       PushChunk( iff, ID_ILBM, ID_BODY, PS );
  300.       WriteChunkBytes( iff, ptr, PS );
  301.       PopChunk( iff );
  302.  
  303.       PopChunk( iff );
  304.       CloseIFF( iff );
  305.       FreeIFF( iff );
  306.     }
  307.     Close( file );
  308.   }
  309.   else
  310.     printf(" - Can't open mask file '%s' !\n", name );
  311. }
  312.  
  313.  
  314. /*------------------------------------------------------------------------*/
  315.  
  316.  
  317. void convertAnim( void )
  318. {
  319.   static char realName[FILENAME_MAX];
  320.   int offset, n;
  321.  
  322.   printf( "Converting an icon animation ...\n" );
  323.   strcpy( realName, fileName );
  324.   for ( offset=AH,n=0 ; offset<fileSize ; offset+=IS,n++ )
  325.   {
  326.     sprintf( fileName, "%s.fr%03d", realName, n );
  327.     convertIcon( &fileBuffer[offset+IH] );
  328.   }
  329.   strcpy( fileName, realName );
  330. }
  331.  
  332.  
  333. /*------------------------------------------------------------------------*/
  334.  
  335.  
  336. void stripFilename( char *name )
  337. {
  338.   static char tmp[FILENAME_MAX];
  339.   int size;
  340.  
  341.   strcpy( tmp, FilePart(name) );
  342.   size = strlen( tmp );
  343.   if ( (size>EXT_SIZE) && (tmp[size-EXT_SIZE]==POINT) )
  344.     tmp[size-EXT_SIZE] = NUL;
  345.   strcpy( name, tmp );
  346. }
  347.  
  348.  
  349. /*------------------------------------------------------------------------*/
  350. /*------------------------------------------------------------------------*/
  351. /*------------------------------------------------------------------------*/
  352. /*------------------------------------------------------------------------*/
  353. /*------------------------------------------------------------------------*/
  354. /*------------------------------------------------------------------------*/
  355. /*------------------------------------------------------------------------*/
  356. /*========================================================================*/
  357. /*=== End ================================================================*/
  358. /*========================================================================*/
  359.